#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/engine/models.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
import json, uuid
from django.db import models
from common.log import logger
CATEGORY_SERVER_TEST = 'tapp'
CATEGORY_SERVER_PROD = 'app'
SERVER_CATEGORY_CHOICES = [
 (
  CATEGORY_SERVER_TEST, u'测试服务器'), (CATEGORY_SERVER_PROD, u'正式服务器')]
THIRD_SERVER_CATEGORY_MQ = 'rabbitmq'
THIRD_SERVER_CATEGORY_CHOICES = [(THIRD_SERVER_CATEGORY_MQ, u'RabbitMQ服务')]

class BkCluster(models.Model):
    name = models.CharField(max_length=20)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'engine_clusters'
        verbose_name = 'cluster info'
        ordering = ('created_at', )


class BkApp(models.Model):
    name = models.CharField(max_length=20)
    logo = models.CharField(max_length=100)
    app_code = models.CharField(max_length=100, unique=True)
    app_lang = models.CharField(max_length=100)
    app_type = models.CharField(max_length=100)
    is_active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def token(self):
        return self.bkapptoken_set.first().key

    @property
    def app_envs(self):
        app_envs = {'test': {},'prod': {}}
        for app_env in self.bkappenv_set.all():
            mode, key, value = app_env.mode, app_env.key, app_env.value
            app_envs[mode][key] = value

        return app_envs

    def __unicode__(self):
        return self.app_code

    class Meta:
        db_table = 'engine_apps'
        verbose_name = 'app info'
        ordering = ('created_at', )


class BkAppToken(models.Model):
    bk_app = models.ForeignKey(BkApp, on_delete=models.CASCADE)
    key = models.UUIDField(default=uuid.uuid4, editable=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.key

    class Meta:
        db_table = 'engine_app_tokens'
        verbose_name = 'app token'
        ordering = ('created_at', )


class BkServerManager(models.Manager):

    def check_test_app_deployable(self):
        return BkServer.objects.filter(category=CATEGORY_SERVER_TEST, is_active=True).exists()

    def check_prod_app_deployable(self):
        return BkServer.objects.filter(category=CATEGORY_SERVER_PROD, is_active=True).exists()

    def check_saas_app_deployable(self):
        return BkServer.objects.filter(category=CATEGORY_SERVER_PROD, is_active=True).exists()


class BkServer(models.Model):
    name = models.CharField(u'名称', max_length=20)
    s_id = models.UUIDField(u'服务ID', default=uuid.uuid4, editable=False)
    token = models.UUIDField(default=uuid.uuid4, editable=False)
    ip_address = models.CharField(u'IP地址', max_length=36)
    ip_port = models.CharField(u'Agent端口', max_length=36, default='4245')
    app_port = models.CharField(u'App端口', max_length=36, default='8085')
    category = models.CharField(u'分类', max_length=36, choices=SERVER_CATEGORY_CHOICES, default=CATEGORY_SERVER_TEST)
    info = models.CharField(u'备注', max_length=200)
    is_active = models.BooleanField(u'启用', default=False)
    apps = models.ManyToManyField(BkApp, blank=True, through='BkHostingShip')
    mac = models.CharField(u'MAC地址', max_length=36, default='')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = BkServerManager()

    class Meta:
        db_table = 'engine_servers'
        verbose_name = u'服务器信息'
        verbose_name_plural = u'服务器信息'
        ordering = ('created_at', )


class ThirdServer(models.Model):
    category = models.CharField(u'分类', max_length=36, choices=THIRD_SERVER_CATEGORY_CHOICES, default=THIRD_SERVER_CATEGORY_MQ)
    server_info = models.TextField(u'服务器信息')
    info = models.CharField(u'备注', max_length=200)
    is_active = models.BooleanField(u'启用', default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def server_data(self):
        try:
            return json.loads(self.server_info)
        except:
            logger.exception(u'获取服务器信息异常')
            return {}

    @property
    def ip_address(self):
        try:
            server_info = json.loads(self.server_info)
            ip_address = server_info.get('ip_address', '')
            return ip_address
        except:
            logger.exception(u'获取服务器IP信息异常')
            return ''

    @property
    def ip_port(self):
        try:
            server_info = json.loads(self.server_info)
            ip_port = server_info.get('ip_port', '')
            return ip_port
        except:
            logger.exception(u'获取服务器端口信息异常')
            return ''

    class Meta:
        db_table = 'engine_third_servers'
        verbose_name = u'第三方服务器信息'
        verbose_name_plural = u'第三方服务器信息'
        ordering = ('created_at', )


class BkHostingShip(models.Model):
    bk_app = models.ForeignKey(BkApp)
    bk_server = models.ForeignKey(BkServer)
    is_active = models.BooleanField(default=True)
    is_master = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'engine_hosting_ships'
        verbose_name = 'router map'
        ordering = ('created_at', )

    def __str__(self):
        return ('{0}-{1}-{2}').format(self.bk_server.ip_address, self.bk_server.ip_port, self.bk_server.is_active)


class BkAppEnv(models.Model):
    bk_app = models.ForeignKey(BkApp)
    mode = models.CharField(max_length=200)
    key = models.CharField(max_length=200)
    value = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'engine_app_envs'
        verbose_name = 'app env'
        ordering = ('created_at', )


class BkAppEvent(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
    bk_app = models.ForeignKey(BkApp)
    event_type = models.CharField(max_length=200)
    status = models.CharField(max_length=200)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def logs(self):
        logs = ''
        for event_log in self.bkappeventlog_set.all():
            logs += event_log.log

        return logs

    class Meta:
        db_table = 'engine_app_events'
        verbose_name = 'app event'
        ordering = ('created_at', )


class BkAppEventLog(models.Model):
    bk_app_event = models.ForeignKey(BkAppEvent)
    log = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'engine_app_event_logs'
        verbose_name = 'app event log'
        ordering = ('created_at', )

    def __str__(self):
        return ('{0}-{1}').format(self.id, self.log)
